#!/bin/bash
# Betsu-WebStatus v0.0.1
# Written by Alexa Hattori for The Betsu Project.
# https://betsu.org/func/status
# Licensed under BSD3-Clause.

# Colors (for echo -e) 

RED='\033[0;31m'
CYAN='\033[0;36m'
NOCOLOR='\033[0m'

# Settings

# Set "/dev/null" as value to disable :)
logfile="/var/log/webstatus.log" # Specify logfile
html_file="/home/alexa/Betsu/Betsu-Tools/experimental/betsu-webstatus/index.html" # Specify html file

check_status () {
  # echo "[ DEBUG ] Betsu-Uptime : Checking.. $1"
  if [[ $(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null) == *"200"* ]]; # Check status of site, if 200 - up, else - down.
  then
      site_status="up" # Set status to "up"
      echo -e "[ $(date +%r) - $(date +%D) ] [  ${CYAN}UP${NOCOLOR}  ] $1" # Output to console: [ 02:09:14 AM - 04/03/23 ] [  UP  ] https://example.org
  else
      site_status="down" # Set status to "up"
      echo -e "[ $(date +%r) - $(date +%D) ] [ ${RED}DOWN${NOCOLOR} ] $1" # Output to console: [ 02:09:14 AM - 04/03/23 ] [ DOWN ] https://example.org
  fi

  site="$1" 
  report $site_status $site # Write to log file and html 
}

report () {
  echo "[ $(date +%r) - $(date +%D) ] [  $site_status  ] $2" >> $logfile # Write "[ 02:09:14 AM - 04/03/23 ] [  (up or down)  ] https://example.org" to log file
  echo "[ $(date +%r) - $(date +%D) ] [  $site_status  ] $2" >> $html_file # Write "[ 02:09:14 AM - 04/03/23 ] [  (up or down)  ] https://example.org" to html file
}

loop () {
  while true
  do
      printf "\033c"
      > $html_file # Clear html file
      echo '<title>Betsu-WebStatus v0.0.1</title>' >> $html_file # Add <title> tag to html file, to set title to "Betsu-WebStatus v0.0.1".
      echo '<meta http-equiv="refresh" content="18" ./index.html>' >> $html_file # Add <meta> tag to html file, to refresh it every 18 seconds (you can edit for your own needs).
      echo '<body style="background-color:black">' >> $html_file # Add <body> tag with style "background-color:black" to set background color - black.
      echo '<pre style="color:white; font-size:30px">' >> $html_file # Add <pre> tag with style "color:white; font-size:30px" to set text color to white.
      # Write sites you want to monitor here
      # Example: check_status https://example.org
      check_status https://betsu.org
      check_status https://catserv.betsu.org
      check_status https://cdn.betsu.org
      check_status https://iv.betsu.org
      check_status https://sx.betsu.org
      sleep 15 # Check every 15 seconds. It's 3:21 AM when I am writing this comment, I need to: sleep 28800 (8 hours) myself, hehe.
  done
}

loop # Start 
